package lib.quasar.widget.menu;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;

import lib.quasar.util.LogUtil;
import lib.quasar.widget.R;
import lib.quasar.widget.paint.PaintManager;

@SuppressLint("AppCompatCustomView")
public final class MenuAutoCompleteTextView extends AutoCompleteTextView {

    private boolean localtionTop = false;
    private boolean localtionBottom = false;
    private boolean localtionLeft = false;
    private boolean localtionRight = false;
    private float lineHeight = 0f;
    private int lineColor = Color.BLACK;

    private float menuWidth = 0f;
    private String menuText;
    private int menuTextColor = Color.BLACK;
    private float menuTextSize = 0f;

    public MenuAutoCompleteTextView(Context context) {
        this(context, null);
    }

    public MenuAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray array = null;

        try {
            array = context.obtainStyledAttributes(attrs, R.styleable.MenuAutoCompleteTextView);
            localtionTop = array.getBoolean(R.styleable.MenuAutoCompleteTextView_mactv_line_top, false);
            localtionBottom = array.getBoolean(R.styleable.MenuAutoCompleteTextView_mactv_line_bottom, false);
            localtionLeft = array.getBoolean(R.styleable.MenuAutoCompleteTextView_mactv_line_left, false);
            localtionRight = array.getBoolean(R.styleable.MenuAutoCompleteTextView_mactv_line_right, false);
            lineHeight = array.getDimension(R.styleable.MenuAutoCompleteTextView_mactv_line_height, lineHeight);
            lineColor = array.getColor(R.styleable.MenuAutoCompleteTextView_mactv_line_color, lineColor);
            menuWidth = array.getDimension(R.styleable.MenuAutoCompleteTextView_mactv_menu_width, menuWidth);
            menuText = array.getString(R.styleable.MenuAutoCompleteTextView_mactv_menu_text);
            menuTextColor = array.getColor(R.styleable.MenuAutoCompleteTextView_mactv_menu_text_color, menuTextColor);
            menuTextSize = array.getDimension(R.styleable.MenuAutoCompleteTextView_mactv_menu_text_size, menuTextSize);
        } catch (Exception e) {
            LogUtil.e("MenuAutoCompleteTextView", e.getMessage(), e);
        } finally {
            if (null == array) return;
            array.recycle();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (menuWidth > 0f && menuTextSize > 0f && !TextUtils.isEmpty(menuText)) {
            final float paddingLeft = Math.max(getPaddingLeft(), menuWidth);
            setPadding((int) paddingLeft, getPaddingTop(), getPaddingRight(), getPaddingBottom());
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (lineHeight > 0f) {

            final int height = getHeight();
            final int width = getWidth();
            final float paddingLeft = getPaddingLeft();
            final int paddingRight = getPaddingRight();
            final int paddingTop = getPaddingTop();
            final int paddingBottom = getPaddingBottom();
            final Paint linePaint = PaintManager.getLinePaint(lineColor, lineHeight);

            if (localtionTop) {
                canvas.drawRect(paddingLeft, 0, width - paddingRight, lineHeight, linePaint);
            }

            if (localtionBottom) {
                canvas.drawRect(paddingLeft, height - lineHeight, width - paddingRight, height, linePaint);
            }

            if (localtionLeft) {
                canvas.drawRect(paddingLeft, paddingTop, lineHeight, height - paddingBottom, linePaint);
            }

            if (localtionRight) {
                canvas.drawRect(width - paddingRight - lineHeight, paddingTop, width - paddingRight, height - paddingBottom, linePaint);
            }
        }

        if (menuWidth > 0f && menuTextSize > 0f && !TextUtils.isEmpty(menuText)) {
            final Paint textPaint = PaintManager.getTextPaint(menuTextColor, menuTextSize);
            final float paddingLeft = getPaddingLeft();
            canvas.drawText(menuText, paddingLeft * 0.5f, getHeight() * 0.5f, textPaint);
        }
    }
}
